package com.lhd.egweibo3.controller;

import com.lhd.egweibo3.common.response.JsonResult;
import com.lhd.egweibo3.common.response.StatusCode;
import com.lhd.egweibo3.mapper.WeiboMapper;
import com.lhd.egweibo3.pojo.dto.WeiboDTO;
import com.lhd.egweibo3.pojo.entity.Weibo;
import com.lhd.egweibo3.pojo.vo.UserVO;
import com.lhd.egweibo3.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author lhd
 * @date 2024/4/25
 * @apiNote
 */
@Api(tags = "2.微博模块")
@Slf4j
@RestController
@RequestMapping(value ="v1/weibo/")
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;
    @ApiOperation("插入微博")
    @PostMapping(value = "insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.debug("微博内容：{}",weiboDTO);

        UserVO userVO = (UserVO)session.getAttribute("user");
        if (userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());

        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }

    @ApiOperation(value ="微博首页功能")
    @PostMapping(value ="selectIndex")
    public JsonResult selectIndex(){
        List<WeiboIndexVO> weiboIndexVOS =weiboMapper.selectIndex();
        return JsonResult.ok(weiboIndexVOS);
    }

    @ApiOperation(value ="微博详情页功能")
    @PostMapping(value = "selectById")
    @ApiImplicitParam(name="id",value = "微博id",required = true, dataType = "Long")
    public JsonResult selectById(Long id){
        WeiboIndexVO weiboIndexVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboIndexVO);
    }



}
